//
//  BasePageView.m
//  SleepApp
//
//  Created by MAC on 2021/3/27.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "BasePageView.h"
#import "BasePageCollectionViewCell.h"

#pragma mark -- BasePageViewController 的内容页
@interface BasePageView ()<WLCollectionDelegate>
@property(nonatomic,strong) WLCollectionView *wlclass_collectionView;
@end
@implementation BasePageView
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        [self addSubview:self.wlclass_collectionView];
        [self.wlclass_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
        }];
    }
    return self;
}
-(void)setNav_view_model:(WLNavigationScrollViewModel *)nav_view_model{
    _nav_view_model = nav_view_model;
    /** 监听当前数据变更 */
    __WEAK_SELF_YLSLIDE
    [RACObserve(_nav_view_model, nav_class_list) subscribeNext:^(id  _Nullable x) {
        if (x != nil && ![self.wlclass_collectionView getAllSection].count){
            NSLog(@"%@",weakSelf);
            [weakSelf.wlclass_collectionView setAllSection:[weakSelf getContentList:x]];
        }
    }];
    /** 联动页面修改 */
    [RACObserve(_nav_view_model, select_index) subscribeNext:^(id  _Nullable x) {
        if (![self.wlclass_collectionView initReload]){
            NSInteger selectIndex = [x integerValue];
            WLLog;
            [self changePageIndex:selectIndex];
        }
    }];
}
#pragma mark -- 内容视图数据
/** 获取内容视图展示模型 */
-(NSArray<WLBaseCollectionViewLayerSection *> *)getContentList:(NSArray<WLNavigationScrollModel *> *)nav_class_list{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (WLNavigationScrollModel *nav in nav_class_list) {
        WLBaseCollectionViewLayerSection *section = [[WLBaseCollectionViewLayerSection alloc]init];
        section.insetForSection = UIEdgeInsetsZero;
        section.horizontalSection = 0;
        section.verticalSection = 0;
        
        WLBaseCollectionViewLayerRow *row = [[WLBaseCollectionViewLayerRow alloc]init];
        row.keyId = [NSString stringWithFormat:@"%@-%ld",NSStringFromClass(nav.viewClass),array.count];
        
        NSLog(@"%@",row.keyId);
        NSLog(@"%@",section.item);
        row.viewClass = nav.viewClass;
        row.cellSize = self.frame.size;
        row.data = nav;
        [section.item addObject:row];
        
        [array addObject:section];
    }
    return array;
}
#pragma mark -- 页面逻辑
-(void)changePageIndex:(NSInteger)pageIndex{
    [self.wlclass_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:pageIndex] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    BasePageCollectionViewCell *cell = (BasePageCollectionViewCell *)[self.wlclass_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:pageIndex]];
    if ([cell isKindOfClass:BasePageCollectionViewCell.class]){
        [cell navigationContentCellShow];
    }
}
#pragma mark -- 代理等
// 换页
- (void)wl_changeDragging:(UIScrollView *)scrollView pageNumber:(NSInteger)pageNumber{
    self.nav_view_model.offset_progress = scrollView.contentOffset.x/scrollView.frame.size.width;
    self.nav_view_model.select_index = pageNumber;
}
// 滚动就会触发
- (void)wl_scrollViewDidScroll:(UIScrollView *)scrollView{
    self.nav_view_model.offset_progress = scrollView.contentOffset.x/scrollView.frame.size.width;
}
#pragma mark -- 懒加载
-(WLCollectionView *)wlclass_collectionView{
    if(!_wlclass_collectionView){
        _wlclass_collectionView = [WLCollectionView initWithHorizontalDelegate:self];
        _wlclass_collectionView.pagingEnabled = YES;
        _wlclass_collectionView.bounces = NO;
    }
    return _wlclass_collectionView;
}
@end
